package com.crudpro.javacrud.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/auth-test")
@RequiredArgsConstructor
@Tag(name = "认证测试", description = "测试JWT认证是否生效的API")
public class AuthTestController {

    @GetMapping
    @Operation(
        summary = "测试认证状态", 
        description = "用于测试JWT认证是否生效，需要在请求头中包含有效的JWT令牌",
        security = { @SecurityRequirement(name = "bearerAuth") }
    )
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "认证成功"),
        @ApiResponse(responseCode = "401", description = "认证失败，令牌无效或已过期")
    })
    public ResponseEntity<Map<String, String>> testAuth(HttpServletRequest request) {
        String username = (String) request.getAttribute("username");
        
        Map<String, String> response = new HashMap<>();
        response.put("message", "You are authenticated");
        response.put("username", username);
        
        return ResponseEntity.ok(response);
    }
} 